package com.example.onlinemusic.controller;


import com.example.onlinemusic.model.User;
import com.example.onlinemusic.service.UserService;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    //注册
    @RequestMapping("/reg")
    public ResponseBodyMessage<Boolean> reg(@RequestParam String username, @RequestParam String password) {
        User user1 = userService.selectByName(username);
        if (user1 != null) {
            return new ResponseBodyMessage<>(-1, "当前用户已存在!", false);
        } else {
            User user2 = new User();
            user2.setUsername(username);
            String password1 = bCryptPasswordEncoder.encode(password);
            user2.setPassword(password1);
            userService.addUser(user2);
            return new ResponseBodyMessage<>(1, "注册成功!", true);
        }
    }


    @RequestMapping("/login1")
    //将请求参数绑定到你控制器的方法参数上 。如果这个参数是非必传的可以写为：@RequestParam(required = false) ，默认是true
    public ResponseBodyMessage<Boolean> login1(@RequestParam String username, @RequestParam String password, HttpServletRequest request) {


        User user = userService.selectByName(username);

        if (user != null) {
            // bCryptPasswordEncoder 采用注入的方式
            boolean flg = bCryptPasswordEncoder.matches(password, user.getPassword());
            if (!flg) {
                return new ResponseBodyMessage<>(-1, "密码错误 ！", false);
            } else {
                System.out.println("登录成功 ！");
                // 登录成功，将用户信息写入Session中
                request.getSession().setAttribute(Constant.USERINFO_SESSION_KEY, user);
                return new ResponseBodyMessage<>(1, "登录成功 ！", true);
            }

        } else {
            System.out.println("登录失败 ！");
            return new ResponseBodyMessage<>(-1, "登录失败 ！", false);
        }
    }


//    @RequestMapping("/login")
//    //将请求参数绑定到你控制器的方法参数上 。如果这个参数是非必传的可以写为：@RequestParam(required = false) ，默认是true
//    public ResponseBodyMessage<Boolean> login(@RequestParam String username, @RequestParam String password, HttpServletRequest request) {
//        User userLogin = new User();
//        userLogin.setUsername(username);
//        userLogin.setPassword(password);
//
//        User user = userService.login(userLogin);
//
//        if (user != null) {
//            System.out.println("登录成功 ！");
//            // 登录成功，将用户信息写入Session中
//            request.getSession().setAttribute(Constant.USERINFO_SESSION_KEY, user);
//            return new ResponseBodyMessage<>(1, "登录成功 ！", true);
//        } else {
//            System.out.println("登录失败 ！");
//            return new ResponseBodyMessage<>(-1, "登录失败 ！", false);
//        }
//    }


    @RequestMapping("/logout")
    public ResponseBodyMessage<Boolean> logout(HttpServletRequest request, HttpServletResponse response) {
        //退出登录则清除session中的用户信息
        HttpSession session = request.getSession(false);
        if (session == null) {
            System.out.println("您还未登录！");
            return new ResponseBodyMessage<>(-1, "您还未登录!", false);
        } else {

            session.removeAttribute(Constant.USERINFO_SESSION_KEY);

            if (session.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
                System.out.println("清除session中的用户信息成功!");
//                try {
//                    response.sendRedirect("login.html");//此处要注意，只需要将后端数据传给前端，让前端来判断是否需要重定向即可
//                } catch (IOException e) {               //若后端也进行重定向，就可能出错 ！！
//                    e.printStackTrace();
//                }
                return new ResponseBodyMessage<>(1, "退出成功!", true);
            }
            return new ResponseBodyMessage<>(-1, "退出失败!", false);
        }
    }
}
